﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarBodyDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarBodyDB
    {
        public CarBodyDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarBody CarBody)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarBodyInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarBodyId", CarBody.ProviderCarBodyId);
            _sPara.Add("@ProviderCompanyId", CarBody.ProviderCompanyId);
            _sPara.Add("@CXBody", CarBody.CXBody);
            _sPara.Add("@BodyCode", CarBody.BodyCode);
            _sPara.Add("@Body", CarBody.Body);
            _sPara.Add("@Rate", CarBody.Rate);
            _sPara.Add("@RefCEUserId", CarBody.RefCEUserId);
            _sPara.Add("@Status", CarBody.Status);
            _sPara.Add("@Finfo1", CarBody.FInfo1);
            _sPara.Add("@Dinfo1", CarBody.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarBody GetProviderCarBody(Int64 ProviderCarBodyId)
        {
            CarBody _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarBodySelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarBodyId", ProviderCarBodyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarBody();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarBody GetProviderCarBody(Int64 ProviderCompanyId, string CXBody)
        {
            CarBody _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarBodyRSASelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXBody", CXBody);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarBody();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarBody GetProviderCarBodyRSA(Int64 ProviderCompanyId, string CXBody, string Body)
        {
            CarBody _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarBodyRSASelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXBody", CXBody);
            _spara.Add("@Body", Body);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarBody();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarBody FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarBody myCarBody = new CarBody();
            myCarBody.ProviderCarBodyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarBodyId"].ToString());
            myCarBody.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarBody.CXBody = myDataTable.Rows[RowId]["CXBody"].ToString();
            myCarBody.BodyCode = myDataTable.Rows[RowId]["BodyCode"].ToString();
            myCarBody.Body = myDataTable.Rows[RowId]["Body"].ToString();
            myCarBody.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarBody.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarBody.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarBody.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());


            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarBody.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarBody.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarBody.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarBody;
        }
    }
}
